<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>策略模式</title>
	</head>
	<body>
		<script>
			// 使用策略模式前, if else 模式
			function askPrice(tag, originPrice) {
				// 处理预热价
				if (tag === 'pre') {
					if (originPrice >= 100) {
						return originPrice - 20
					}
					return originPrice * 0.9
				}
				// 处理大促价
				if (tag === 'onSale') {
					if (originPrice >= 100) {
						return originPrice - 30
					}
					return originPrice * 0.8
				}

				// 处理返场价
				if (tag === 'back') {
					if (originPrice >= 200) {
						return originPrice - 50
					}
					return originPrice
				}

				// 处理尝鲜价
				if (tag === 'fresh') {
					return originPrice * 0.5
				}

				// 处理新人价
				if (tag === 'newUser') {
					if (originPrice >= 100) {
						return originPrice - 50
					}
					return originPrice
				}
			}
			
			// 使用策略模式后
			// 定义一个询价处理器对象
			const priceProcessor = {
			  pre(originPrice) {
			    if (originPrice >= 100) {
			      return originPrice - 20;
			    }
			    return originPrice * 0.9;
			  },
			  onSale(originPrice) {
			    if (originPrice >= 100) {
			      return originPrice - 30;
			    }
			    return originPrice * 0.8;
			  },
			  back(originPrice) {
			    if (originPrice >= 200) {
			      return originPrice - 50;
			    }
			    return originPrice;
			  },
			  fresh(originPrice) {
			    return originPrice * 0.5;
			  },
			};
			// 询价函数
			function askPrice(tag, originPrice) {
			  return priceProcessor[tag](originPrice)
			}
			// 如果需要新增的话
			priceProcessor.newUser = function (originPrice) {
			  if (originPrice >= 100) {
			    return originPrice - 50;
			  }
			  return originPrice;
			}
		</script>
	</body>
</html>
